package org.real.guetshop.mapper;

import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.real.guetshop.annotation.AutoFill;
import org.real.guetshop.enumeration.OperationType;
import org.real.guetshop.pojo.dto.ProductQueryDTO;
import org.real.guetshop.pojo.entity.Category;
import org.real.guetshop.pojo.entity.Product;
import org.real.guetshop.pojo.vo.ProductVO;

import java.util.List;

@Mapper
public interface ProductMapper {

    /**
     * 新增商品
     * @param product
     */
    @AutoFill(OperationType.INSERT)
    @Insert("insert into products " +
            "(name, category_id, price, status, image,store_id, description, total, unit, create_user, create_time, update_user, update_time) " +
            "values " +
            "(#{name},#{categoryId},#{price},#{status},#{image},#{storeId},#{description},#{total},#{unit},#{createUser},#{createTime},#{updateUser},#{updateTime})")
    void insert(Product product);

    /**
     * 修改商品
     * @param product
     */
    @AutoFill(OperationType.UPDATE)
    void updateProduct(Product product);

    /**
     * 根据id查询商品
     * @param productId
     * @return
     */
    @Select("select * from products where id = #{productId}")
    Product getById(Integer productId);

    /**
     * 商品批量删除
     * @param ids
     */
    void deleteByIds(List<Integer> ids);

    /**
     * 商品分页查询
     * @param productQueryDTO
     * @return
     */
    Page<Product> pageQuery(ProductQueryDTO productQueryDTO);

    /**
     * 根据分类id查询商品
     * @param product
     * @return
     */
    List<Product> queryByCategoryId(Product product);

    /**
     * 修改商品状态
     * @param id
     * @param status
     */
    @Update("update products set status = #{status} where id = #{id}")
    void editStatus(Integer id, Integer status);

    /**
     * 根据商店id查询商品
     * @param product
     * @return
     */
    List<Product> queryByStoreId(Product product);

    /**
     * 根据分类id查询商品
     * @param product
     * @return
     */
    List<Product> list(Product product);

    /**
     * 获取分类列表
     * @return
     */
    @Select("select * from category")
    List<Category> getCategory();

    /**
     * 获取热销商品
     * @return
     */
    @Select("select p.*,s.name as storeName,c.type as categoryName from products p " +
            "INNER JOIN store s on s.id = p.store_id " +
            "INNER JOIN category c on c.id = p.category_id " +
            "order by sold_number desc limit 5")
    List<ProductVO> getHot();

    /**
     * 下单后减少库存
     */
    @Update("update products set total = total-#{number},sold_number = #{number} where id = #{productId}")
    void updateProductTotal(Integer number, Integer productId);

    /**
     * 获取全部商品
     * @return
     */
    @Select("select p.*,c.type as categoryName,s.name as storeName from products p " +
            "INNER join category c on p.category_id = c.id" +
            " INNER join store s on p.store_id = s.id")
    List<Product> findAllProducts();

    /**
     * 获取商品库存
     * @param productId
     * @return
     */
    @Select("select total from products where id = #{productId}")
    Integer getTotalByProductId(Integer productId);
}
